package com.base.myproject.client.HR.bcode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.LasyObejct;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.CommandFactory;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class Bcode_Train extends LayoutContainer implements LasyObejct{
	BaseStore<BusiObject> mainstore = null;
	Button seach_b = new Button("查询");
	public Bcode_Train(BaseStore<BusiObject> mainstore) {
		this.mainstore = mainstore;
		init();
		//this.setStyleAttribute("background-color", "#DFE8F6");
	}
	void init() {
		initwidget();
	}
	Widget initwidget() {

		VerticalPanel v = new VerticalPanel();
		v.setSpacing(2);
		
		TabPanel tabpanel = new TabPanel();
		tabpanel.setPlain(true);
		tabpanel.setHeight(450);
		tabpanel.setWidth(740);
		TabItem t1 = new TabItem();
		t1.setText("已培训");
		VerticalPanel vt1 = new VerticalPanel();
		vt1.setSpacing(0);
		
		vt1.add(seachForm());
		vt1.add(createGrid());
		t1.add(vt1);
		t1.setScrollMode(Scroll.AUTO);
		
		TabItem t2 = new TabItem();
		t2.setText("未培训");
		t2.add(create_noGrid());
		t2.setScrollMode(Scroll.AUTO);
		tabpanel.add(t2);
		tabpanel.add(t1);

		//v.add(fs1);
		v.add(tabpanel);
		this.add(v);
		return null;
	}
	/******/
	BaseStore<BusiObject> store = null;
	BaseStore<BusiObject> getStore(){
	if(store==null) 
	{
		createStore();
	}
	return store;}
	BaseStore<BusiObject> createStore(){
	 ModelType type = new ModelType();
	    type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("typeCode");
		type.getField("typeCode").setType(String.class);
		type.addField("typeName");
		type.getField("typeName").setType(String.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("number");
		type.getField("number").setType(Integer.class);
		type.addField("attendanced");
		type.getField("attendanced").setType(Boolean.class);
		type.addField("content");
		type.getField("content").setType(String.class);
		type.addField("trainingDate");
		type.getField("trainingDate").setType(Date.class);
		type.addField("period");
		type.getField("period").setType(Integer.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("Train_v");
		info.setKeyname(new String[] { "icode"});
		info.setSql("select * from Train_v where bcode='{bcode}' and attendanced=0");//bcode='{bcode}'
		
		info.setParent(mainstore);// 关联主表
		info.setLinkparentcolumns(new String[] { "bcode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "bcode" });// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
			
	 return store;
	 }
	
	BaseStore<BusiObject> store_no = null;
	BaseStore<BusiObject> get_noStore(){
	if(store_no==null) 
	{
		create_noStore();
	}
	return store_no;}
	BaseStore<BusiObject> create_noStore(){	
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("typeCode");
		type.getField("typeCode").setType(String.class);
		type.addField("typeName");
		type.getField("typeName").setType(String.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("number");
		type.getField("number").setType(Integer.class);
		type.addField("attendanced");
		type.getField("attendanced").setType(Boolean.class);
		type.addField("content");
		type.getField("content").setType(String.class);
		type.addField("trainingDate");
		type.getField("trainingDate").setType(Date.class);
		type.addField("period");
		type.getField("period").setType(Integer.class);
		StoreInfo info = new StoreInfo();

		info.setTablename("Train_v");
		info.setKeyname(new String[] { "icode"});
		info.setSql(" select * from Train_v where bcode='{bcode}' and attendanced=1 ");//bcode='{bcode}'
		
		info.setParent(mainstore);// 关联主表
		info.setLinkparentcolumns(new String[] { "bcode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "bcode" });// 关联主表字段
		store_no = new BaseStore<BusiObject>(type, info);
		
		
		return store_no;	
	 
	 }
	/********/
	BaseGrid grid=null;
	BaseGrid createGrid(){
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		
		ColumnConfig C7 = new ColumnConfig("typeName", "培训类型名称", 90);
		columns.add(C7);
		
		ColumnConfig C5 = new ColumnConfig("bcode", "参培人员", 60);
		columns.add(C5);
		
		ColumnConfig C8 = new ColumnConfig("trainingDate", "培训日期", 80);
		C8.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C8);
		
		ColumnConfig C6 = new ColumnConfig("content", "培训类容", 80);
		columns.add(C6);
		
		ColumnConfig C1 = new ColumnConfig("number", "培训期数", 60);
		//NumberField num = new NumberField();
		//C1.setNumberFormat(NumberFormat.getFormat("00"));
		columns.add(C1);
		
		ColumnConfig C2 = new ColumnConfig("period", "培训时长", 80);
		columns.add(C2);
		
		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(get_noStore(), cm ,false,false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setHeight(370);
		grid.setWidth(736);
		grid.setBorders(false);
		return grid ;
		
	}
	/****/
	
	BaseGrid grid_no=null;
	BaseGrid create_noGrid(){
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		
		ColumnConfig C7 = new ColumnConfig("typeCode", "培训类型名称", 90);
		columns.add(C7);
		
		//ColumnConfig C5 = new ColumnConfig("bcode", "参培人员", 60);
		//columns.add(C5);
		
		//ColumnConfig C8 = new ColumnConfig("entryDate", "培训日期", 80);
		//C8.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		//columns.add(C8);
		
		ColumnConfig C6 = new ColumnConfig("insidePosition", "培训类容", 80);
		columns.add(C6);
		
		ColumnConfig C1 = new ColumnConfig("number", "培训期数", 60);
		NumberField num = new NumberField();
		C1.setNumberFormat(NumberFormat.getFormat("00"));
		columns.add(C1);
		
		ColumnConfig C2 = new ColumnConfig("sectionCode", "培训时长", 80);
		columns.add(C2);
		
		ColumnModel cm = new ColumnModel(columns);
		grid_no = new BaseGrid(getStore(), cm,false,false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid_no.setSelectionModel(cbs);
		grid_no.setHeight(400);
		grid_no.setWidth(738);
		grid_no.setBorders(false);
		return grid_no ;
	}
	/***seach**/
	DateField C_data15 = new DateField();
	DateField C_data16 = new DateField();
	BaseComboBoxForm<BaseModelData> C_num26 ;
	TextField<String> C_num27;
	BaseFormPanel seachForm() {
		BaseFormPanel form = new BaseFormPanel(mainstore);
		form.setBodyBorder(false);
		form.setStyleName("form_css2");
		form.setPadding(2);
		TableLayout tl = new TableLayout(9);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.MIDDLE);
		td.setPadding(1);

		form.setLayout(tl);
		form.setHeaderVisible(false);
		
		form.add(new Label("开始时间:"), td);
		C_data15.setWidth(100);
		C_data15.getPropertyEditor().setFormat(	DateTimeFormat.getFormat("yyyy-M-d"));
		form.add(C_data15, td);
		
		form.add(new Label("结束时间:"), td);
		C_data16.setWidth(100);
		C_data16.getPropertyEditor().setFormat(	DateTimeFormat.getFormat("yyyy-M-d"));
		form.add(C_data16, td);
		
		form.add(new Label("培训类型:"), td);
		C_num26 =new BaseComboBoxForm<BaseModelData>("typeCode","typeName","hr_TrainingTypeDefine","",true);
		C_num26.setWidth(110);
		form.add(C_num26, td);
		
		form.add(new Label("培训类容:"), td);
		C_num27 = new TextField<String>();
		C_num27.setWidth(110);
		form.add(C_num27, td);
		seach_b.addListener(Events.Select, getListener());
		form.add(seach_b, td);
		
		form.inited();
		return form;
	}
	
	
	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(seach_b)) {
								String b_date=C_data15.getRawValue();
								String e_date=C_data16.getRawValue();
								String train_name=C_num26.getRawValue().trim();//.getValue().get("code");
								String train_contant=C_num27.getRawValue().trim();
                               if(b_date.equals("") && e_date.equals("") && train_name.equals("") && train_contant.equals("")){
                            	   Info.display("提示", "请至少输入一个条件!");
                            	   return ;
                               }else{
                            	    String where="";
                            	    if(!b_date.equals("") && !e_date.equals("")){
                            		    where+=" and trainingDate  between '"+b_date+"' and '"+e_date+"'";
                            	    }
									if(!train_name.equals("")){
										where+=" and typeName ='"+train_name+"'";                         		   
									}
									if(!train_contant.equals("")){
										where+=" and content like '%25"+train_contant+"%25'";
									}
									String bcode=mainstore.getValue("bcode").toString();
		                            String sql="select * from Train_v where bcode='"+bcode+"' and attendanced=1 " +where;
		                            grid.getStore().getStoreinfo().setSql(sql);
		                            grid.getStore().reload();
		                            grid.getView().refresh(true);
                               }
                               
                               
							}
						} else if (be.getType() == Events.Changed) {
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
	
	
	

	@Override
	public boolean isopen() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean open() {
		// TODO Auto-generated method stub
		return false;
	}
}
